--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"

require "common"

local AssertEx = AssertEx

local Command = class("Command")
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine)
	self.msg = message 
	self.machine = machine 
	self.close  = false
	self.tableid = self.machine:getTableId()
	self.gameid = getGameIdbyTable(self.tableid)
	self:register()
end

--注册本服务里的消息
function Command:register()

	self.msg:registerSelf('safe_close',handler(self,self.safeClose))
	self.msg:registerSelf('update_prop',handler(self,self.updateProperty))
	self.msg:registerSelf('gm_set_code_req',handler(self,self.gmSet)) --配牌

	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))	
	self.msg:registerSelf('lost_connect',handler(self,self.lostConnect))
	self.msg:registerSelf('reconnect_req',handler(self,self.reconnect)) 

	self.msg:registerSelf('bjl_status_req',handler(self,self.getStatus))
	self.msg:registerSelf('bjl_bet_req',handler(self,self.bet))
	self.msg:registerSelf('bjl_rebet_req',handler(self,self.rebet))
	self.msg:registerSelf('bjl_remain_card_req',handler(self,self.remainCard))

end

---------------------------------------------------------
-- CMD
---------------------------------------------------------

--更新属性
function Command:updateProperty(playerid, data)
	print("_________________大厅发来玩家数据___",data)
	self.machine:updateProperty(playerid,data)
	return true
end

--安全关服
function Command:safeClose(close)
	self.close = close
	self.machine:setClose(close)
end


---------------------------------------------------------
-- REQUEST
---------------------------------------------------------
--进入桌子
function Command:enterTable(playerid,player_info)
	AssertEx(not self.close, 'enter_table_res' , 7)
	AssertEx(playerid > 0 , 'enter_table_res' , 8)
	local player = self.machine:getPlayer(playerid)
	if player then		
		if player.disconnect then --断线重连进来						
		else
			--没有断线重复请求进入桌子	
			print("_____没有断线重复请求进入桌子________")
		end
	else
		--第一次进入
		player = table.copy(player_info)
		self.machine:addPlayer(player)
		self.machine:updatePlayerNum()
		--符合条件，自动坐下
		-- local bSeat = self.machine:sitDown(playerid)
		-- if not bSeat then 
		-- 	print("座位已满")
		-- end
	end
	player.disconnect = false
	local result = {}
	result.enter_table_res = {
		tableid = self.tableid,
		gameid = self.gameid,
	}
	return result
end

--离开
function Command:leaveTable(playerid)
	--print("___________bjl_leave_table_req_________1_____")
	local player = self.machine:getPlayer(playerid)
	if player then										
		if not player.playing  or self.machine:getState() == 'rest' then 
			self.machine:leaveTable(playerid)
			return {leave_table_res = {tableid = self.tableid}}
		end		
		return {leave_table_res = GetErrorTable(9)}
	else
		return {leave_table_res = GetErrorTable(10)}		
	end
	return false
end

--掉线
function Command:lostConnect(playerid)
	local kickPlayer = false
	local player = self.machine:getPlayer(playerid)
	if player then
		player.disconnect = true		
		kickPlayer = self.machine:disconnect(playerid)
	else
		--log.error('>>>>>>>>>>>>>>>>>>>>############lost_connect no found playerid:',playerid)
		print('>>>>>>>>>>>>>>>>>>>>############玩家掉线，但游戏中不存在此玩家:',playerid)
	end
	return kickPlayer
end

--重连
function Command:reconnect(playerid,args)
	local result = {}
	local player = self.machine:getPlayer(playerid)
	AssertEx(player , 'reconnect_res', 10)

end

-- 得到桌子状态
function Command:getStatus( playerid )
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'bjl_status_res', 10)
	--重连进入
	player.disconnect = false
	local status = self.machine:getTableStatus(playerid)
	return {bjl_status_res = status}
end


--GM命令
function Command:gmSet( playerid,cards ,repeatNum)
	self.machine:setGmCard(playerid,cards,repeatNum)

	--return {gm_set_card_res = {}}
end
------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------
--下注
function Command:bet(playerid,args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'bjl_bet_res', 10)	
	local res = self.machine:onPlayerBet(playerid,args)
	if not res then 
		return 
	end	
	return {bjl_bet_res = res}	
end

--重复下注
function Command:rebet(playerid,args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'bjl_rebet_res', 10)	
	local res = self.machine:onPlayerRebet(playerid)
	if not res then 
		return 
	end
	return {bjl_rebet_res = res}	
end

--查看剩余的牌
function Command:remainCard(playerid)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'bjl_remain_card_res', 10)	
	local status = self.machine:onRemainCard(playerid)
	return {bjl_remain_card_res = status}	
end




return Command